package desmontec.alertas

import desmontec.aluguel.AluguelMaquina;
import desmontec.aluguel.AluguelMaquinaService
import desmontec.aluguel.ConsumoAluguel;
import desmontec.sistema.SecUser;
import desmontec.testes.TesteIntegracao;
import desmontec.maquinas.Maquina

class AlertaMaquinaManutencaoControllerTest extends TesteIntegracao {
	static transactional = false
	AlertaMaquinaService alertaMaquinaService
	AluguelMaquinaService aluguelMaquinaService
	protected void setUp() {
		super.setUp()
	}
	
	protected void tearDown() {
		super.tearDown()
	}
	
	
	void testInserirDados(){
		this.inserirUsuario()
		this.inserirUsuario2()
		this.inserirCliente1()
		this.inserirCliente2()
		this.inserirMaquina1()
		this.inserirMaquina2()
		this.inserirMaquina3()
		this.inserirAluguel1()
		this.inserirAluguel2()
		this.inserirAluguel3()
		this.inserirConsumosAluguel1()
		this.inserirConsumosAluguel3()
	}
	
	void testCriacaoAlerta(){
		
		AluguelMaquina aluguel = AluguelMaquina.get(1);
		
		print ConsumoAluguel.findAllByAluguel(aluguel).size
		
		assertEquals 0, AlertaMaquinaManutencao.findAll().size
		
		alertaMaquinaService.atualizaAlertasManutencao(aluguel)
		
		assertEquals 1, AlertaMaquinaManutencao.findAll().size
		
		alertaMaquinaService.atualizaAlertasManutencao(AluguelMaquina.get(2))
		
		assertEquals 1, AlertaMaquinaManutencao.findAll().size
		
	
	}
    void testListaAlugueis() {
		
		def alertas = alertaMaquinaService.alertasDoUsuario(SecUser.get(1))
		assertFalse alertas == null
		assertEquals 1, alertas.size()
		for (AlertaMaquinaManutencao a in alertas){
			assertTrue a.maquina ==  AluguelMaquina.get(1).maquina
		}
		
		alertas = alertaMaquinaService.alertasDoUsuario(SecUser.get(2))
		assertEquals 0, alertas.size()
		
	}

	void testAlertaAluguelInativo() {
		
		AluguelMaquina aluguel = AluguelMaquina.get(1)
		Calendar novaDataInicio = GregorianCalendar.getInstance()
		novaDataInicio.add(Calendar.MONTH, -12);
		
		Calendar novaDataFim = GregorianCalendar.getInstance()
		novaDataFim.add(Calendar.MONTH, -10);
		aluguel.inicioValidade = novaDataInicio.getTime()
		aluguel.fimValidade = novaDataFim.getTime()
		aluguel.flagAtivo = "N"
		aluguel.save(flush: true)
	
		alertaMaquinaService.atualizaAlertasManutencao(aluguel)
		
		assertEquals 0, AlertaMaquinaManutencao.findAll().size
	
	}
	
	void testRemoverAlertasManutencao() {
		AluguelMaquina aluguel = AluguelMaquina.get(3)
		
		alertaMaquinaService.atualizaAlertasManutencao(aluguel)
		
		def alertas = alertaMaquinaService.alertasDoUsuario(SecUser.get(1))
		assertFalse alertas == null
		assertEquals 1, alertas.size()
		for (AlertaMaquinaManutencao a in alertas){
			assertTrue a.maquina ==  aluguel.maquina
		}
		
		alertaMaquinaService.removeAlertasManutencao(aluguel)
		
		alertas = alertaMaquinaService.alertasDoUsuario(SecUser.get(1))
		assertFalse alertas == null
		assertEquals 0, alertas.size()
		
	}
	

}
